﻿using System;
using System.Web.Security;

using VIT.Web.Controls;
using VIT.Web.UI;
using VIT.Library;

namespace VIT.Web.Admin
{
    public partial class Login : VITPage
    {        
        Provider.Account acc;
        protected void Page_Load(object sender, EventArgs e)
        {
            // Logout
            if (!String.IsNullOrEmpty(Request["logout"]))
            {
                Session["userName"] = String.Empty;
                logout.Visible = true;
            }
            
            //Log in
            acc = new Provider.Account();
            string usr = Request["username"];
            string pwd = Request["passwd"];
            if (usr != null || pwd != null)
            {
                pwd = pwd.EnCodeMD5();
                if (login(usr, pwd))
                {
                    FormsAuthentication.SetAuthCookie(usr, false);
                    Response.Redirect(linkComponent("Home"));
                }
                else notvalid.Visible = true;
            }
            
            // user: administrator
            // pass: 290893326
            //acc.setAdministratorRoles();
        }

        private bool login(string usr, string pwd)
        {
            acc.UserName = usr;
            acc.Password = pwd;
            if (acc.Login())
            {
                Session["FullName"] = acc.FullName;
                Session["userName"] = acc.UserName;
                Session["userRoles"] = acc.Roles;
                return true;
            }
            return false;
        }
    }
}